import { Controller, Post, Body, HttpStatus, Get, Query } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterUserDto } from './dto/register-user.dto';
import {
  ApiOperation,
  ApiTags,
  ApiResponse,
  ApiQuery,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { PublicVoSuccess, PublicVoError } from 'src/utils/publicVo/index';
import { LoginUserDto } from './dto/login-user.dto';
import { RequireLogin } from 'src/middleware/auth/custom.decorator';
import { UserListVo } from './vo/user-list.vo';
import { UserLoginVo } from './vo/login-user-vo';

@ApiTags('用户中心')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('register')
  @ApiOperation({ summary: '用户注册', description: '用户注册接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async register(@Body() registerUser: RegisterUserDto) {
    return await this.userService.register(registerUser);
  }

  @Get('register/email')
  @ApiOperation({
    summary: '注册邮箱验证码',
    description: '注册邮箱验证码接口',
  })
  @ApiQuery({
    name: 'email',
    type: String,
    description: '邮箱地址',
    required: true,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async registerEmail(@Query('email') email: string) {
    return await this.userService.registerEmail(email);
  }

  @Post('login')
  @ApiOperation({
    summary: '登录',
    description: '登录接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserLoginVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async userLogin(@Body() loginUserDto: LoginUserDto) {
    return await this.userService.userLogin(loginUserDto);
  }

  @Get('login/svg')
  @ApiOperation({
    summary: '登录图形验证码',
    description: '登录图形验证码接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async registerSvg() {
    return await this.userService.registerSvg();
  }

  @Get()
  @ApiOperation({
    summary: '用户列表',
    description: '用户列表接口',
  })
  @ApiQuery({
    name: 'pageNum',
    type: Number,
    description: '页码',
    required: true,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '页数',
    required: true,
  })
  @ApiQuery({
    name: 'username',
    type: String,
    description: '用户名',
    required: false,
  })
  @ApiQuery({
    name: 'nickName',
    type: String,
    description: '昵称',
    required: false,
  })
  @ApiQuery({
    name: 'email',
    type: String,
    description: '邮箱',
    required: false,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: UserListVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  @ApiBearerAuth()
  @RequireLogin()
  async userList(
    @Query('username') username: string,
    @Query('nickName') nickName: string,
    @Query('email') email: string,
    @Query('pageNum') pageNum: number,
    @Query('pageSize') pageSize: number,
  ) {
    return await this.userService.userList(
      username,
      nickName,
      email,
      pageNum,
      pageSize,
    );
  }
}
